package com.tianbing.adlottery.controller.hordeadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyHordeProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyHordeProfileController.class.getName());

	private IUserDao userDao;

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		User2Site user2Site = userBusiness.findUserManagedHorde(auth.getUserSysId(), -1);
		Site site = null;
		if (user2Site == null) {
			site = new Site();
		} else {
			site = user2Site.getSite();
		}
		fb.setSite(site);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", null);
		Site site = form.getSite();

		userBusiness.saveUserManagedHorde(auth.getUserSysId(), site, sgid);
		skinUtils.refreshUserAuthorization(request, response);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);

		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", null);
		if (sgid != null) {
			List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(sgid);
			model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
			model.put("sgid", sgid);
		}
		return model;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	private boolean isDemoUser(UserSession authorization) {
		if (authorization != null && authorization.getUserId() != null
				&& authorization.getUserId().equals("demo@500hao.com"))
			return true;
		return false;
	}

	public static void setLogger(Logger logger) {
		EditMyHordeProfileController.logger = logger;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
}
